package com.guiji.text2video.fastvideo.task;

import cn.hutool.core.date.DateField;
import cn.hutool.core.date.DateUtil;
import com.guiji.text2video.fastvideo.config.ExecutorConfig;
import com.guiji.text2video.fastvideo.domain.vo.Text2videoJob;
import com.guiji.text2video.fastvideo.domain.vo.Text2videoPoolsStat;
import com.guiji.text2video.fastvideo.mapper.Text2videoPoolsStatMapper;
import com.guiji.text2video.fastvideo.service.IText2videoJobService;
import com.guiji.text2video.fastvideo.service.IText2videoPoolsStatService;
import com.guiji.text2video.fastvideo.service.IText2videoZipJobService;
import com.guiji.text2video.fastvideo.config.ApplicationContextConfig;
import com.guiji.text2video.fastvideo.utils.ObjectUtils;
import com.guiji.text2video.fastvideo.utils.PriorityRunnable;
import com.guiji.text2video.fastvideo.utils.VisiableThreadPoolTaskExecutor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.List;
import java.util.concurrent.Executor;

/**
 * @program: ruoyi 定时任务
 * @description:
 * @author: suncl
 * @create: 2021-05-27 18:09
 **/

@Component("poolsStatTimingTask")
public class PoolsStatTimingTask {

    @Autowired
    IText2videoPoolsStatService iText2videoPoolsStatService;

    @Autowired(required = false)
    Text2videoPoolsStatMapper pools;

    @Autowired
    IText2videoJobService text2videoJobService;

    @Autowired
    IText2videoZipJobService iText2videoZipJobService;

    @Scheduled(cron = "0/2 * * * * *")
    public void upgradePoolsStat() {
        VisiableThreadPoolTaskExecutor asyncNLPServiceExecutor = (VisiableThreadPoolTaskExecutor) ApplicationContextConfig.getBean("asyncNLPServiceExecutor");
        asyncNLPServiceExecutor.execute(null);

        VisiableThreadPoolTaskExecutor asyncTTSServiceExecutor = (VisiableThreadPoolTaskExecutor) ApplicationContextConfig.getBean("asyncTTSServiceExecutor");
        asyncTTSServiceExecutor.execute(null);

        VisiableThreadPoolTaskExecutor asyncComposeVideoServiceExecutor = (VisiableThreadPoolTaskExecutor) ApplicationContextConfig.getBean("asyncComposeVideoServiceExecutor");
        asyncComposeVideoServiceExecutor.execute(null);

        VisiableThreadPoolTaskExecutor asyncComposeVideoNewsServiceExecutor = (VisiableThreadPoolTaskExecutor) ApplicationContextConfig.getBean("asyncComposeVideoNewsServiceExecutor");
        asyncComposeVideoNewsServiceExecutor.execute(null);
    }
    /**
     * @descp: 每10分钟删除 线程池的一小时之前的内容
     * @param:
     * @Author: scl
     * @Date: 2021/8/12 14:24
     */
    @Scheduled(cron = "0 0/10 * * * ?")
    public void deletePoolStat() {
        Date nowDate = DateUtil.offset(new Date(), DateField.HOUR, -1);
        Text2videoPoolsStat pools = new Text2videoPoolsStat();
        pools.setCreateTime(nowDate);
        iText2videoPoolsStatService.deleteOneHourAgo(pools);
    }
}
